/*
 * Created by JFormDesigner on Mon Jun 19 19:39:09 CST 2023
 */

package fm.report;

import dao.Dao_money;
import model.Report_in;
import model.Report_out;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author YangC
 */
public class ReportMoney extends JInternalFrame {
    private String [] columns = {"出库明细编号", "商品","类型", "码洋", "实洋", "出库时间"};
    private String [] column = {"入库明细编号", "商品", "类型", "码洋", "实洋", "入库时间"};

    private Object [][] getFS(java.util.List list)
    {
        Object [][] results = new Object[list.size()][columns.length];

        for(int i = 0; i < list.size(); i++)
        {
            Report_out report_out =  (Report_out) list.get(i);

            results[i][0] = report_out.getO_no();
            results[i][1] = report_out.getC_name();
            results[i][2] = report_out.getCs_name();
            results[i][3] = report_out.getO_price();
            results[i][4] = report_out.getO_realPrice();
            results[i][5] = report_out.getO_time();

        }
        return results;
    }

    private Object [][] getFS_in(List list)
    {
        Object [][] results = new Object[list.size()][column.length];

        for(int i = 0; i < list.size(); i++)
        {
            Report_in report_in =  (Report_in) list.get(i);

            results[i][0] = report_in.getI_no();
            results[i][1] = report_in.getC_name();
            results[i][2] = report_in.getCs_name();
            results[i][3] = report_in.getI_price();
            results[i][4] = report_in.getI_realsPrice();
            results[i][5] = report_in.getI_time();

        }
        return results;
    }

    public ReportMoney() {
        initComponents();
        iniDate();
    }

    public void iniDate() {

        List<String> timeList = new ArrayList<>();
        timeList.addAll(Dao_money.getInTime());
        timeList.addAll(Dao_money.getOutTime());

        Set<String> timeSet = new HashSet<>(timeList);
        timeComboBox.removeAllItems();
        for (String time : timeSet) {
            timeComboBox.addItem(time);
        }

        String time = (String) timeComboBox.getSelectedItem();

        Object[][] results = getFS(Dao_money.select_outMoney(time));
        DefaultTableModel model_1 = new DefaultTableModel(results, columns);
        outTable.setModel(model_1);


        Object[][] result = getFS_in(Dao_money.select_inMoney(time));
        DefaultTableModel model_2 = new DefaultTableModel(result, column);
        inTable.setModel(model_2);
        }


        private void cancel (ActionEvent e){
            doDefaultCloseAction();
        }

        private void time(ActionEvent e) {
            String time = (String) timeComboBox.getSelectedItem();

            Object[][] results = getFS(Dao_money.select_outMoney(time));
            DefaultTableModel model_1 = new DefaultTableModel(results, columns);
            outTable.setModel(model_1);


            Object[][] result = getFS_in(Dao_money.select_inMoney(time));
            DefaultTableModel model_2 = new DefaultTableModel(result, column);
            inTable.setModel(model_2);
        }

        private void count(ActionEvent e) {
            int setRow_in = inTable.getRowCount();
            int setRow_out = outTable.getRowCount();
            double reals;
            double outReals = 0;
            double inReals = 0;

            if(setRow_out != 0 && setRow_in != 0)
            {
                if(setRow_out < setRow_in)
                {
                    for(int i = 0; i < setRow_in; i++)
                    {

                        double inReal = Double.parseDouble(inTable.getValueAt(i, 4).toString().trim());


                        inReals += inReal;
                    }

                    for(int j = 0; j < setRow_out; j++)
                    {
                        double outReal = Double.parseDouble(outTable.getValueAt(j, 4).toString().trim());
                        outReals += outReal;
                    }
                }

                if(setRow_out > setRow_in)
                {
                    for(int i = 0; i < setRow_out; i++)
                    {
                        double outReal = Double.parseDouble(outTable.getValueAt(i, 4).toString().trim());

                        outReals += outReal;
                    }
                    for(int j = 0; j < setRow_in; j++)
                    {
                        double inReal = Double.parseDouble(inTable.getValueAt(j, 4).toString().trim());

                        inReals += inReal;
                    }

                }

                if(setRow_out == setRow_in)
                {
                    for(int i = 0; i < setRow_out; i++)
                    {
                        double outReal = Double.parseDouble(outTable.getValueAt(i, 4).toString().trim());
                        double inReal = Double.parseDouble(inTable.getValueAt(i, 4).toString().trim());

                        outReals += outReal;
                        inReals += inReal;
                    }
                }

                reals = inReals + outReals;
                countTextField.setText(String.valueOf(reals));

            }

            if(setRow_out != 0 && setRow_in == 0)
            {
                for(int i = 0; i < setRow_out; i++)
                    {
                        double outReal = Double.parseDouble(outTable.getValueAt(i, 4).toString().trim());

                        outReals += outReal;
                    }

                reals = outReals;
                countTextField.setText(String.valueOf(reals));
            }

            if(setRow_out == 0 && setRow_in != 0)
            {
                for(int i = 0; i < setRow_in; i++)
                {
                    double inReal = Double.parseDouble(inTable.getValueAt(i, 4).toString().trim());

                    inReals += inReal;
                }
                reals = inReals;
                countTextField.setText(String.valueOf(reals));
            }

        }

        private void initComponents () {
            // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
            panel1 = new JPanel();
            cancelButton = new JButton();
            panel4 = new JPanel();
            timeComboBox = new JComboBox();
            panel5 = new JPanel();
            label1 = new JLabel();
            countTextField = new JTextField();
            countButton = new JButton();
            tabbedPane1 = new JTabbedPane();
            scrollPane2 = new JScrollPane();
            outTable = new JTable();
            scrollPane3 = new JScrollPane();
            inTable = new JTable();

            //======== this ========
            setVisible(true);
            setClosable(true);
            setIconifiable(true);
            setTitle("\u8d22\u52a1\u62a5\u8868");
            var contentPane = getContentPane();
            contentPane.setLayout(new BorderLayout());

            //======== panel1 ========
            {
                panel1.setPreferredSize(new Dimension(327, 40));
                panel1.setLayout(null);

                //---- cancelButton ----
                cancelButton.setText("\u8fd4\u56de");
                cancelButton.addActionListener(e -> cancel(e));
                panel1.add(cancelButton);
                cancelButton.setBounds(new Rectangle(new Point(286, 5), cancelButton.getPreferredSize()));

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel1.getComponentCount(); i++) {
                        Rectangle bounds = panel1.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel1.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel1.setMinimumSize(preferredSize);
                    panel1.setPreferredSize(preferredSize);
                }
            }
            contentPane.add(panel1, BorderLayout.SOUTH);

            //======== panel4 ========
            {
                panel4.setLayout(new BorderLayout());

                //---- timeComboBox ----
                timeComboBox.addActionListener(e -> time(e));
                panel4.add(timeComboBox, BorderLayout.NORTH);

                //======== panel5 ========
                {
                    panel5.setPreferredSize(new Dimension(0, 50));
                    panel5.setLayout(null);

                    //---- label1 ----
                    label1.setText("\u4eca\u65e5\u8425\u6536\uff1a");
                    panel5.add(label1);
                    label1.setBounds(97, 16, label1.getPreferredSize().width, 18);
                    panel5.add(countTextField);
                    countTextField.setBounds(157, 11, 80, countTextField.getPreferredSize().height);

                    //---- countButton ----
                    countButton.setText("\u8ba1\u7b97\u4eca\u65e5\u8425\u6536");
                    countButton.addActionListener(e -> count(e));
                    panel5.add(countButton);
                    countButton.setBounds(new Rectangle(new Point(400, 12), countButton.getPreferredSize()));

                    {
                        // compute preferred size
                        Dimension preferredSize = new Dimension();
                        for(int i = 0; i < panel5.getComponentCount(); i++) {
                            Rectangle bounds = panel5.getComponent(i).getBounds();
                            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                        }
                        Insets insets = panel5.getInsets();
                        preferredSize.width += insets.right;
                        preferredSize.height += insets.bottom;
                        panel5.setMinimumSize(preferredSize);
                        panel5.setPreferredSize(preferredSize);
                    }
                }
                panel4.add(panel5, BorderLayout.SOUTH);

                //======== tabbedPane1 ========
                {

                    //======== scrollPane2 ========
                    {
                        scrollPane2.setViewportView(outTable);
                    }
                    tabbedPane1.addTab("\u51fa\u5e93", scrollPane2);

                    //======== scrollPane3 ========
                    {
                        scrollPane3.setViewportView(inTable);
                    }
                    tabbedPane1.addTab("\u5165\u5e93", scrollPane3);
                }
                panel4.add(tabbedPane1, BorderLayout.CENTER);
            }
            contentPane.add(panel4, BorderLayout.CENTER);
            // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
        }

        // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
        private JPanel panel1;
        private JButton cancelButton;
        private JPanel panel4;
        private JComboBox timeComboBox;
        private JPanel panel5;
        private JLabel label1;
        private JTextField countTextField;
        private JButton countButton;
        private JTabbedPane tabbedPane1;
        private JScrollPane scrollPane2;
        private JTable outTable;
        private JScrollPane scrollPane3;
        private JTable inTable;
        // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    }
